package com.jsh.erp.service.rebateRule;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.datasource.entities.RebateMaterial;
import com.jsh.erp.datasource.entities.RebateRule;
import com.jsh.erp.datasource.entities.RebateRuleExample;
import com.jsh.erp.datasource.mappers.RebateRuleMapper;
import com.jsh.erp.datasource.mappers.RebateRuleMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RebateRuleService {
    private Logger logger = LoggerFactory.getLogger(RebateRuleService.class);

    @Resource
    private RebateRuleMapper rebateRuleMapper;

    @Resource
    private RebateRuleMapperEx rebateRuleMapperEx;
    @Resource
    private SystemConfigService systemConfigService;

    public RebateRule getRebateRule(String id)throws Exception {
        RebateRule result=null;
        try{
            result=rebateRuleMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public List<RebateRule> getRebateRule()throws Exception {
        RebateRuleExample example = new RebateRuleExample();
        example.createCriteria();
        List<RebateRule> list=null;
        try{
            list=rebateRuleMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<RebateRule> select(String rebateRuleId, int offset, int rows)throws Exception {
        List<RebateRule> list=null;
        try{
            list=rebateRuleMapperEx.selectByConditionRebateRule(rebateRuleId, offset, rows);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }


    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertRebateRule(JSONObject obj, HttpServletRequest request) throws Exception{
        RebateRule rebateRule = JSONObject.parseObject(obj.toJSONString(), RebateRule.class);
        rebateRule.setUploadFlag("0");
        int result=0;
        try{
            result=rebateRuleMapper.insertSelective(rebateRule);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateRebateRule(JSONObject obj, HttpServletRequest request) throws Exception{
        RebateRule rebateRule = JSONObject.parseObject(obj.toJSONString(), RebateRule.class);
        rebateRule.setUploadFlag("0");
        int result=0;
        try{
            result = rebateRuleMapper.updateByPrimaryKeySelective(rebateRule);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteRebateRule(String id, HttpServletRequest request)throws Exception {
        RebateRule rebateRule = new RebateRule();
        rebateRule.setId(id);
        rebateRule.setDeleteFlag("1");
        rebateRule.setUploadFlag("0");
        int result=0;
        try{
            result=rebateRuleMapper.updateByPrimaryKeySelective(rebateRule);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteRebateRule(String ids, HttpServletRequest request)throws Exception {
        List<String> idList = StringUtil.strToStringList(ids);
        RebateRuleExample example = new RebateRuleExample();
        example.createCriteria().andIdIn(idList);
        RebateRule rebateRule = new RebateRule();
        rebateRule.setDeleteFlag("1");
        rebateRule.setUploadFlag("0");
        int result=0;
        try{
            result=rebateRuleMapper.updateByExampleSelective(rebateRule,example);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

//    public int updateRebateRuleByKey(String platformKey, String platformValue)throws Exception {
//        int result=0;
//        try{
//            RebateRule rebateRule = new RebateRule();
//            rebateRule.setPlatformValue(platformValue);
//            RebateRuleExample example = new RebateRuleExample();
//            example.createCriteria().andPlatformKeyEqualTo(platformKey);
//            result = rebateRuleMapper.updateByExampleSelective(rebateRule, example);
//        }catch(Exception e){
//            JshException.writeFail(logger, e, systemConfigService.getLanCode());
//        }
//        return result;
//    }

    public RebateRule getRebateRuleByKey(String rebateRuleId)throws Exception {
        RebateRule rebateRule = new RebateRule();
        try{
            RebateRuleExample example = new RebateRuleExample();
            example.createCriteria().andRebateIdEqualTo(rebateRuleId);
            List<RebateRule> list=rebateRuleMapper.selectByExample(example);
            if(list!=null && list.size()>0){
                rebateRule = list.get(0);
            }
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return rebateRule;
    }

    public Long countRebateRule(String rebateRuleId) {
        Long result = null;
        try {
            result = rebateRuleMapperEx.countRebateRule(rebateRuleId);
        } catch (Exception e) {
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }
}
